import {request} from "@/utils/request";
import {LoginUserModel} from "./data";
import {ApiResult} from "@/utils/data";

export default {
  /** 用户登录 */
  async login(data: LoginUserModel): Promise<ApiResult<{token:string}>> {
    return request.post('/api/v2/sys/account/login', {data});
  },

  /** 检查账号登录是否需要验证码 */
  async requireCaptcha(account: string): Promise<ApiResult<boolean>> {
    return request.post('/api/v2/sys/account/requireCaptcha', {data: {account}});
  },

  /** 获取验证码 */
  async captcha(account: string, width: number = 140, height: number = 40, type: 'math' | 'gif' | 'chinese' = 'math'): Promise<any> {
    return request.post('/api/v2/sys/account/captcha', {
      data: {account, width, height, type},
      responseType: 'arrayBuffer',
    }).then(res => `data:image/png;base64,${btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`).catch(ex => {
      console.error(ex);
    });
  },
}
